﻿using System;
using System.Diagnostics;
using System.Drawing.Imaging;

namespace Universe.Drawing
{
    class CodecUtils
    {

        /*
        557cf400-1a04-11d3-9a73-0000f81ef32e: image/bmp, *.BMP;*.DIB;*.RLE, Built-in BMP Codec
        557cf401-1a04-11d3-9a73-0000f81ef32e: image/jpeg, *.JPG;*.JPEG;*.JPE;*.JFIF, Built-in JPEG Codec
        557cf402-1a04-11d3-9a73-0000f81ef32e: image/gif, *.GIF, Built-in GIF Codec
        557cf405-1a04-11d3-9a73-0000f81ef32e: image/tiff, *.TIF;*.TIFF, Built-in TIFF Codec
        557cf406-1a04-11d3-9a73-0000f81ef32e: image/png, *.PNG, Built-in PNG Codec
        */
        public static void ShowInfo()
        {
            
            ImageCodecInfo[] codecs = ImageCodecInfo.GetImageEncoders();
            foreach (ImageCodecInfo info in codecs)
            {

                Trace.WriteLine(info.Clsid + ": " + info.MimeType + ", " + info.FilenameExtension + ", " + info.CodecName);
            }

        }

        // Builtin formats of GDI+ in human readable form
        public enum ImgFormat
        {
            Unknown,
            MemoryBmp,
            Bmp,
            Emf,
            Wmf,
            Jpeg,
            Png,
            Gif,
            Tiff,
            Exif,
            Icon,
        }

        // Transforms .net ImageFormat into human readble enum
        public static ImgFormat GetImageFormat(ImageFormat imageFormat)
        {
            if (imageFormat == null)
                throw new ArgumentNullException("imageFormat");

            var all = new object[]
            {
                ImageFormat.Bmp,         ImgFormat.Bmp,       
                ImageFormat.Png,         ImgFormat.Png,       
                ImageFormat.Jpeg,        ImgFormat.Jpeg,      
                ImageFormat.MemoryBmp,   ImgFormat.MemoryBmp, 
                ImageFormat.Emf,         ImgFormat.Emf,       
                ImageFormat.Exif,        ImgFormat.Exif,      
                ImageFormat.Gif,         ImgFormat.Gif,       
                ImageFormat.Icon,        ImgFormat.Icon,      
                ImageFormat.Tiff,        ImgFormat.Tiff,      
                ImageFormat.Wmf,         ImgFormat.Wmf,       
            };

            for(int i=0; i<=all.Length; i += 2)
            {
                ImageFormat candidate = (ImageFormat) all[i];
                ImgFormat ret = (ImgFormat) all[i + 1];
                if (imageFormat.Equals(candidate))
                    return ret;
            }

            return ImgFormat.Unknown;
        }
    }
}
